package com.atguigu.architecture;

import com.atguigu.pojo.WordCount;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.experimental.SocketStreamIterator;
import org.apache.flink.util.Collector;

public class Flink01_Parallelism {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",5678);

        //1.创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        //2.设置并行度
        env.setParallelism(1);

        //3.从数据源读取数据
        DataStreamSource<String> ds = env.socketTextStream("hadoop102", 9999);

        //4.切分，分组，求和
        SingleOutputStreamOperator<WordCount> flatMapds = ds.flatMap(
                new FlatMapFunction<String, WordCount>() {
                    @Override
                    public void flatMap(String line, Collector<WordCount> out) throws Exception {
                        String[] words = line.split(" ");
                        for (String word : words){
                            out.collect(new WordCount(word,1L));
                        }
                    }
                }
        ).setParallelism(2)
         .keyBy(
                WordCount :: getWord
        ).sum("count").setParallelism(3);

        flatMapds.print().setParallelism(4);

        //5.执行程序

        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //5.执行
    }
}
